var currentTab;
var recentTab;

var recentTabs = {
	go: function(d) {
		d.addStartingTabToRegistry();
		d.startWatchingTabs();
		d.registerShortcuts();
	},
};

var recentTabsShortcuts = {
	register : function (d) {
		shortcut.add('Ctrl+E', d.goToMostRecentTab);
	}
};

var tabWatcher = {
	watch : function() {
		var container = gBrowser.tabContainer;
		container.addEventListener('TabSelect', tabWatcher.tabSelected, false);
	},
	tabSelected : function() {
		var tab = gBrowser.selectedTab;
		tabRegistry.add(tab);
	},	
};

var tabRegistry = {
	add : function(tab) {
		recentTab = currentTab;
		currentTab = tab;
	},
	get : function() {
		return recentTab;
	},
};

var mostRecentTab = {
	focus : function(d) {
		gBrowser.selectedTab = tabRegistry.get();
	},
};

var system = {
	startWatchingTabs: tabWatcher.watch,
	addStartingTabToRegistry : tabWatcher.tabSelected,
	goToMostRecentTab: function() { mostRecentTab.focus(this); },
	registerShortcuts: function() { recentTabsShortcuts.register(this); }
};

window.addEventListener('load', function(e) { recentTabs.go(system); }, false);

